/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package IDSNN_1_Business;

import com.heatonresearch.book.introneuralnet.neural.feedforward.FeedforwardLayer;
import com.heatonresearch.book.introneuralnet.neural.feedforward.FeedforwardNetwork;
import com.heatonresearch.book.introneuralnet.neural.feedforward.train.Train;
import com.heatonresearch.book.introneuralnet.neural.feedforward.train.backpropagation.Backpropagation;
import javax.swing.JTextArea;

/**
 *
 * @author francisco
 */
public class RedeConta {

        
        private final FeedforwardNetwork network;
        private Train train;
        private JTextArea _reportTextBox;

	public RedeConta(JTextArea _reportTextBox) {
		network = new FeedforwardNetwork();
		network.addLayer(new FeedforwardLayer(13));
		network.addLayer(new FeedforwardLayer(13));
		network.addLayer(new FeedforwardLayer(13));
		network.addLayer(new FeedforwardLayer(1));
		network.reset();
                this._reportTextBox =_reportTextBox;
        }


        public RedeConta(FeedforwardNetwork network){
            this.network= network;
        }
       


        public void trainNN(double[][] inputTest, double[][] idealTest){
		// train the neural network
		train = new Backpropagation(network, inputTest, idealTest, 0.3, 0.3);
                int epoch=0;
		do 
                {
                    train.iteration();
                    if(epoch%100==0)
                            System.out.println("Epoch #" + epoch + " Error:" + train.getError());
                    epoch++;
                }while((train.getError() > 0.01) && epoch<3000);
        }


        public int testNN(double[] inputComp, String ips, String ipd){
		int attack =0;
		double actual[] = network.computeOutputs(inputComp);
                if(actual[0]>0.5){
                _reportTextBox.append("IP Source: "+ips +" - IP Destination: "+ipd+"\n");
                _reportTextBox.append(inputComp[0] + "," + inputComp[1] + "," + inputComp[2] + "," + inputComp[3] + "," + inputComp[4] + "," + inputComp[5] + "," + inputComp[6] + "," + inputComp[7]
                                        + ", actual=" + actual[0]+"\n");
                attack=1;
                }
               /* if((ips.contains("toshiba") || ipd.contains("toshiba")) &&ipd!=null && ips!=null)
                    System.out.println(inputComp[0] + "," + inputComp[1] + "," + inputComp[2] + "," + inputComp[3] + "," + inputComp[4] + "," + inputComp[5] + "," + inputComp[6] + "," + inputComp[7]
                                        + ", actual=" + actual[0]+"\n");*/
		return attack;	
	}

        private FeedforwardNetwork get(){
            return network.cloneStructure();
        }
}